<?php

namespace Proyecto\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class SecurityController extends Controller
{
    /**
     * Carga la plantilla de login.
     * @Route("/", name="login_a")
     * @Route("/login", name="login_b")
     * @Template("ProyectoMainBundle:Security:login.html.twig")
     */
    public function loginAction()
    {
        if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
           return $this->redirectAction();
        }
        else {
        $request = $this->getRequest();
        $session = $request->getSession();

        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(
                SecurityContext::AUTHENTICATION_ERROR
            );
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }

        return array(
                // last username entered by the user
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'         => $error,
            );
        }
    }
    
       /**
     * Carga el home de visitante.
     *
     * @Route("/home", name="home")
     * @Template()
     */
    public function redirectAction()
    {
        $user = $this->getUser();
        if ($user){
            return $this->redirect($this->generateUrl($user->getPerfil()->getPerfil()));
        }
        else{
            return $this->redirect($this->generateUrl('login_b'));
        }
        
                
    }
    
    /**
     * Carga el home de visitante.
     *
     * @Route("/visitante", name="visitante")
     * @Route("/user", name="user")
     * @Route("/admin", name="admin")
     * @Route("/secretario", name="secretario")
     * @Template("ProyectoMainBundle:Layout:layout.html.twig")
     */
    public function homeAction(){
        return array();
    }
    
    
}
